package com.cn.wanxi.servlet.product;

import com.alibaba.fastjson.JSONObject;
import com.cn.wanxi.model.ProductModel;
import com.cn.wanxi.service.IProductService;
import com.cn.wanxi.service.impl.ProductServiceImpl;
import com.cn.wanxi.util.Upload;
import org.apache.commons.fileupload.FileItem;
import org.apache.commons.fileupload.disk.DiskFileItemFactory;
import org.apache.commons.fileupload.servlet.ServletFileUpload;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.tools.Tool;
import java.io.File;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.util.List;

@WebServlet("/back/product/add")
public class ProductAddServlet extends HttpServlet {
    @Override
    protected void doPost(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
//        0.处理乱码
        req.setCharacterEncoding("UTF-8");
        resp.setCharacterEncoding("UTF-8");

//        String title = req.getParameter("title");
//        String cuisine=req.getParameter("cuisine");
//        String imgHref=req.getParameter("imgHref");
//        String content=req.getParameter("content");
//        System.out.println(title);
        Upload upload = new Upload();
        String string = upload.imgUpload(req);
        String aa[]=string.split("@@");
        ProductModel productModel=new ProductModel();
        productModel.setCuisineId(Integer.parseInt(aa[0]));
        productModel.setTitle(aa[1]);
        productModel.setImgHref(aa[2]);
        productModel.setContent(aa[3]);


//        調用服务逻辑层
        IProductService iProductService=new ProductServiceImpl();
        int count=iProductService.add(productModel);
//        返回数据
        JSONObject jsonObject = new JSONObject();
        jsonObject.put("count", count);
        resp.getWriter().println(jsonObject);
//        req.getRequestDispatcher("/jsp/aa.jsp").forward(req,resp);

    }
//    }
}
